package com.fulu.javabase.juc.lock;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class ReentrantLockTest {
    private static final Lock lock = new ReentrantLock();
    public static void main(String[] args) {
        new Thread(ReentrantLockTest::test, "t1").start();
        new Thread(ReentrantLockTest::test, "t2").start();
    }

    public static void test(){
        try {
            lock.lock();
            System.out.println(Thread.currentThread().getName() + ": 我获取了锁");
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
            System.out.println(Thread.currentThread().getName() + ": 我释放了锁");
        }
    }
}
